<template>
<div style="display: flex;justify-content: center">

  <div style="display: block">
    <div v-for="item in release" style="display: flex;width: 1000px;padding-top: 20px;padding-bottom: 10px">
      <div class="version-info" style="border-right: 1px solid #dce3e8;width: 200px;display: flex;padding-top: 50px">
        <div style="display: flex;margin-right: 10px">发行版</div>
        <el-icon><CollectionTag /></el-icon>
        <div>{{item.tagVersion}}</div>
      </div>
      <div class="release-info" style="display: flex;justify-content: center">
        <div style="display: block">
          <div style="display: flex;padding-top: 10px;padding-left:30px;justify-content: start;width: 800px;font-size: 15px;font-weight: bold">
            标题
          </div>
          <div style="display: flex;padding-top: 10px;padding-left:30px;justify-content: start;width: 800px;font-size: 25px">
            {{item.tagTitle}}
          </div>
          <div style="display: flex;padding-top: 10px;padding-left:30px;justify-content: start;width: 800px;font-size: 15px;font-weight: bold">
            简介
          </div>
          <div style="display: flex;padding-left:30px;padding-top: 20px;justify-content: start;width: 800px;min-height: 250px">
            {{item.description}}
          </div>
          <div style="display: flex;padding-top: 10px;padding-left:30px;justify-content: start;width: 800px;font-size: 15px;font-weight: bold">
            <el-icon style="margin-top: 4px;margin-right: 5px"><Download /></el-icon>
            下载
          </div>
          <div class="download-box" style="display: flex;justify-content: start;width: 800px">
            <el-link  style="padding-left:40px;padding-top: 10px" :underline="false" @click="download(item.tagVersion)">
              下载Source code(zip)
            </el-link>
          </div>
        </div>
      </div>
    </div>
  </div>
  <el-button v-if="isManager===true" style="float: right;margin-top:30px;margin-left: 20px" type="warning" @click="EnternewRelease">+新建发行版</el-button>
</div>
</template>

<script>
import {CollectionTag,Download } from '@element-plus/icons-vue';
import {Edit} from '@element-plus/icons-vue';
import request from "@/utils/request";
export default {
  name: "releaseView",
  component:{
    Edit,
  },
  data(){
    return{
      isManager:"",
      release:[
      ]
    }
  },
  created(){
    this.repoName=this.$route.params.repoName
    this.ownerName=this.$route.params.userName
    this.load()
  },
  methods:{
    load(){
      request.post("/manager/check",  {"repoName":this.repoName,"managerId":sessionStorage.getItem("userId"),"ownerName":this.ownerName}).then(res => {
        console.log(res)
        this.isManager=res.data
      })
      request.get("/"+this.ownerName+"/"+this.repoName+"/releases").then(res => {
        console.log(res)
        this.release=res.data
      })
    },
    download(tagVersion){
      console.log(tagVersion)
      let targetURL=this.$route.path.replace(/releases/,"download")+'/'+tagVersion
      request.get(targetURL,{
        responseType:'blob'
      }).then(response => {
        console.log(response)
        let blob = new Blob([response], { type: "application/zip;charset-UTF_8" });
        let url = window.URL.createObjectURL(blob);
        const link = document.createElement("a"); // 创建a标签
        link.href = url;
        link.download = tagVersion+'.zip'; // 重命名文件
        link.click();
        URL.revokeObjectURL(url); // 释放内存
      })
          .catch(function (err){
            console.log(err);
          })
    },
    EnternewRelease(){
      this.$router.push({
        path:'/'+sessionStorage.getItem("userName")+'/'+sessionStorage.getItem("repoName")+'/releases/new'
      })
    }
  },
  components: {
    CollectionTag,
    Download
  }
}
</script>

<style scoped>

</style>
